//
//  CViewController.m
//  页面之间传值
//
//  Created by MAC on 2018/8/27.
//  Copyright © 2018年 MAC. All rights reserved.
//

#import "CViewController.h"
//导入B页面的.h类名
#import "C-2-ViewController.h"

@interface CViewController ()
//声明全局UI指针
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation CViewController
//UILabel的懒加载
- (UILabel *)label
{
    if (_label == nil) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 40)];
        _label.backgroundColor = [UIColor blackColor];
        _label.textColor = [UIColor whiteColor];
        _label.font = [UIFont systemFontOfSize:20];
    }
    return _label;
}
//UIButton的懒加载
- (UIButton *)btn
{
    if (_btn == nil) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.backgroundColor = [UIColor redColor];
        _btn.frame = CGRectMake((self.view.frame.size.width-200)/2, _label.frame.origin.y+_label.frame.size.height+40, 200, 40);
        [_btn setTitle:@"跳转页面二" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //点击事件
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   //把UI指针添加到View视图中
    [self.view addSubview:self.label];
    [self.view addSubview:self.btn];
    
}

//button点击事件 -- 跳转到B页面
- (void)btnClick
{
    //创建类指针，并初始化
    C_2_ViewController *c_2_VC = [[C_2_ViewController alloc]init];
    //跳转到B页面
    [self.navigationController pushViewController:c_2_VC animated:YES ];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //接收B页面 -  反向传值
    //如果在接收传值后，发现已经有值了，不要紧张这是存储一次，每次运行都会显示这个值。如果你不想已经页面就显示，那么可以先在赋值前做一个判断，如果key中有值就先清除，然后在进到页面二中存储一个，然后在返回到页面一
    
    self.label.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"NSUserDefaults-re"];
}

@end
